$(document).ready(function() {
	$.ajax({
		url :localhost +  "user/getUser",
		dataType : "jsonp",
		jsonp : "jsonpCallback",
		success : function(data) {
			if (data != null && data != "") {
				$('#userName').val(data.userName);
				$('#userEmail').val(data.userEmail);
				$('#userAbout').val(data.userAbout);
			}  else {
				window.location.href = "sign-in.shtml";
			}
		}
	});

});
$("#userBtn").click(function() {
	var userName = $.trim($('#userName').val());
	var userEmail = $.trim($('#userEmail').val());
	var userAbout = $.trim($("#userAbout").val());
	if (userName == "" || userEmail == "" || userAbout == "") {
		$('#error').text('信息不能为空').show();
		return;
	} else {
		$.ajax({
			url :localhost +  "user/update",
			dataType : "jsonp",
			data : {
				userName : userName,
				userEmail : userEmail,
				userAbout : userAbout
			},
			jsonp : "jsonpCallback",
			success : function(data) {
				if (data <= 0) {
					$("#error").html("*修改失败*");
				} else{
					$("#error").html("*修改成功*");
					$.ajax({
						url :localhost +  "user/getUser",
						dataType : "jsonp",
						jsonp : "jsonpCallback",
						success : function(data) {
							if (data != null && data != "") {
								$('#userName').val(data.userName);
								$('#userEmail').val(data.userEmail);
								$('#userAbout').val(data.userAbout);
							}  else {
								window.location.href = "sign-in.shtml";
							}
						}
					});
				}
			}
		});
	}
});
$("#passwordBtn").click(function() {
	var oldPassword = $.trim($('#oldPassword').val());
	var newPassword = $.trim($('#newPassword').val());
	if (newPassword == "") {
		$('#errorPassword').text('密码不能为空').show();
		return;
	} else {
		$.ajax({
			url :localhost +  "user/updatePassword",
			dataType : "jsonp",
			data : {
				oldPassword : oldPassword,
				newPassword : newPassword
			},
			jsonp : "jsonpCallback",
			success : function(data) {
				if (data <= 0) {
					$("#errorPassword").html("*修改密码失败*");
				} else{
					$("#errorPassword").html("*修改密码成功*");
				}
			}
		});
	}
});